package com.mingqi.enetconnected.util;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.util.Log;

import com.mingqi.enetconnected.util.hint.ToastUtils;

import java.util.ArrayList;
import java.util.List;

/**
 * @author li
 * @version 1.0
 * @describe 本地地图标记工具
 * @WeChat 18571658038
 * @date 2018/9/7 星期五.
 */

public class Jump2MapUtils {

    private Context mContext;
    private String mAppName = "ENetConnected";
    private String mAddressName;
    private double mLat;
    private double mLon;

    private Jump2MapUtils() {
    }

    public static class MapHolder{

        private static Jump2MapUtils INSTANCE = new Jump2MapUtils();
    }

    public static Jump2MapUtils getInstance(){

        return MapHolder.INSTANCE;
    }

    /**
     * 先百度，不存在高德
     * @param context
     * @param addressName 位置名称
     * @param lat 纬度
     * @param lon 经度
     */
    public void  signMap(Context context, String addressName, double lat, double lon) {
        mContext = context;
        mAddressName = addressName;
        mLat = lat;
        mLon = lon;

        sign2BaiDu();
    }


    /**
     * 标记百度
     */
    private void sign2BaiDu() {

        if(isInstallMap(mContext, "com.baidu.BaiduMap")) {

            // 自定义打点
            String markerDescribe = "描述";
            String uri = "baidumap://map/marker?location=" +
                    mLon +
                    "," +
                    mLat +
                    "&title=" +
                    mAddressName +
                    "&content=" +
                    markerDescribe +
                    "&traffic=on&src=" +
                    "andr.sanshang.ENetConnected";
            Intent intent = new Intent();
            intent.setData(Uri.parse(uri));
            mContext.startActivity(intent);

        } else {

            sign2GaoDe();
            Log.d("--TAG--", "Jump2MapUtils sign2BaiDu()" + "没有安装百度地图客户端");
        }
    }

    /**
     * 标记高德
     */
    private void sign2GaoDe() {

        if(isInstallMap(mContext, "com.autonavi.minimap")) {

            String uri = "androidamap://viewMap?sourceApplication=" +
                    mAppName +
                    "&poiname=" +
                    mAddressName +
                    "&lat=" +
                    mLat +
                    "&lon=" +
                    mLon +
                    "&dev=0";
            Intent intent = new Intent();
            intent.setAction("android.intent.action.VIEW");
            intent.addCategory("android.intent.category.DEFAULT");
            intent.setData(Uri.parse(uri));
            intent.setPackage("com.autonavi.minimap");
            mContext.startActivity(intent);
        } else {

            ToastUtils.tMessage("手机内没有安装可支持的地图");
        }
    }

    /**
     * 检查手机上是否安装了指定的软件
     * @param context
     * @param packageName 应用包名
     * @return
     */
    public static boolean isInstallMap(Context context, String packageName) {

        final PackageManager packageManager = context.getPackageManager();
        //获取所有已安装程序的包信息
        List<PackageInfo> packageInfos = packageManager.getInstalledPackages(0);
        //用于存储所有已安装程序的包名
        List<String> packageNames = new ArrayList<>();
        if (packageInfos != null) {
            for (int i = 0; i < packageInfos.size(); i++) {
                String packName = packageInfos.get(i).packageName;
                packageNames.add(packName);
            }
        }
        return packageNames.contains(packageName);
    }
}
